using System;
using System.Data;
using System.Net;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class FTPDeleteFiles : MissionPlugin
    {
        [Package(Direction.Input, "Defines the access settings for a ftp server.")]
        enum FTP_Settings
        {
            [Column(typeof(string), "The directory, where you like to delete files.")]
            DirectoryURLPath,
            [Column(typeof(string), "The user name associated with the ftp server.")]
            UserName,
            [Column(typeof(string), "The password for the user name associated with the ftp server.")]
            Password
        }

        [Package(Direction.Input, "Defines all the files you like to delete.")]
        enum DeleteFiles
        {
            [Column(typeof(string), "The name of the file you like to delete.")]
            FileName
        }

        [Plugin(PluginDevStatus.Production, "Deletes all given files from the expected ftp server.")]
        public FTPDeleteFiles()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        protected override void Work()
        {            
            // get the ftp settings
            DataTableReader ftpSettings = this.GetDataReader(typeof(FTP_Settings));
            while (ftpSettings.Read())
            {
                // get all download files
                DataTableReader ftpFiles = this.GetDataReader(typeof(DeleteFiles));
                while (ftpFiles.Read())
                {
                    string ftpFullPath = ftpSettings[FTP_Settings.DirectoryURLPath.ToString()].ToString() + "/" + ftpFiles[DeleteFiles.FileName.ToString()].ToString();                    
                    
                    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpFullPath);
                    ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                    ftpRequest.Credentials = new NetworkCredential(ftpSettings[FTP_Settings.UserName.ToString()].ToString(), ftpSettings[FTP_Settings.Password.ToString()].ToString());
                    ftpRequest.UsePassive = true;
                    ftpRequest.UseBinary = true;
                    ftpRequest.KeepAlive = false;

                    FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
                    if (FtpStatusCode.FileActionOK == response.StatusCode)
                    {
                        this.AddNewLoggingMessage(typeof(DeleteFiles), String.Format("delete file '{0}'", ftpFullPath));
                    }
                    else
                    {
                        this.AddNewLoggingError(String.Format("can not delete file '{0}' - {1}", ftpFullPath, response.StatusDescription));
                    }
                    response.Close();
                    ftpRequest.Abort();
                }
            }
        }  
    }
}
